package com.zztl.infiniteBand.ui.activity

import android.content.Intent
import android.net.Uri
import android.view.View
import butterknife.OnClick
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.base.BaseMvpActivity
import com.zztl.infiniteBand.model.TradingDetailBean
import com.zztl.infiniteBand.net.requestBean.GetTranscationDetailRequest
import com.zztl.infiniteBand.presenter.TranscationPresenter
import com.zztl.infiniteBand.storage.Constans
import com.zztl.infiniteBand.ui.view.CommonItemView
import com.zztl.infiniteBand.ui.view.dialog.ExitDialog
import com.zztl.infiniteBand.ui.view.mvpview.TranscationDetailView
import com.zztl.infiniteBand.util.PermissionUtils
import com.zztl.infiniteBand.util.UiUtils
import kotlinx.android.synthetic.main.activity_record_detail.*

class RecordDetailActivity : BaseMvpActivity<TranscationDetailView,TranscationPresenter>(), TranscationDetailView {


  /*  @BindView(R.id.tv_money)
    lateinit var tv_money: TextView
    @BindView(R.id.tv_desc)
    lateinit var tv_desc: TextView

    @BindView(R.id.item_time)
    lateinit var item_time: CommonItemView
    @BindView(R.id.item_explain)
    lateinit var item_explain: CommonItemView
    @BindView(R.id.item_device)
    lateinit  var item_device: CommonItemView
    @BindView(R.id.item_phone)
    lateinit  var item_phone: CommonItemView*/


    private var mRecordId: String? = null



    lateinit var mPresenter: TranscationPresenter


    private var mPhoneDialog: ExitDialog? = null


    private val mPermissionGrant = object : PermissionUtils.PermissionGrant {
        override fun onPermissionGranted(requestCode: Int) {
            when (requestCode) {
                PermissionUtils.CODE_CALL_PHONE -> try {
                    showPhoneDialog()
                } catch (e: Exception) {

                }
            }
        }
    }

    override fun setLayoutId(): Int= R.layout.activity_record_detail


    override fun initBase() {
        setTitle(getString(R.string.奖励详情))
        mRecordId = intent.getStringExtra("recordId")
        item_phone.rightTextStr = Constans.CONTACT_PHONE_NUM
        item_phone.setItemRightClickListener(object : CommonItemView.OnItemRightClickListener {
            override fun onClickItemRight(viewID: Int) {
                PermissionUtils.requestPermission(this@RecordDetailActivity, PermissionUtils.CODE_CALL_PHONE, mPermissionGrant)
            }
        })
        if (null != mRecordId) {
            mPresenter.getData(GetTranscationDetailRequest(mRecordId!!), this)
        }
    }

    override fun createPresenter(): TranscationPresenter {
        mPresenter = TranscationPresenter()
        return mPresenter
    }

    override fun getSuccess(bean: TradingDetailBean) {
        tv_money.text = "+" + bean.data!!.transactionMoney
        item_time.rightTextStr = bean.data!!.confirmTimeStr
        item_explain.rightTextStr = bean.data!!.transactionTheme
        item_device.rightTextStr = bean.data!!.equipmentName
        tv_desc.text = bean.data!!.transactionExplain

    }

    @OnClick(R.id.item_phone)
    fun click(view: View) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_CALL_PHONE, mPermissionGrant)
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>,
                                            grantResults: IntArray) {
        PermissionUtils.requestPermissionsResult(this, requestCode, permissions, grantResults, mPermissionGrant)

    }


    private fun showPhoneDialog() {
        if (null == mPhoneDialog) {
            mPhoneDialog = ExitDialog.Builder(this).setMessage(UiUtils.getString(R.string.是否拨打电话) + Constans.CONTACT_PHONE_NUM)
                    .setClickListener(object : ExitDialog.Builder.ClickListener {
                        override fun cancel() {

                        }

                        override fun comfirm() {
                            callPhone(Constans.CONTACT_PHONE_NUM)
                        }
                    }).create()
        }

        mPhoneDialog!!.show()
    }

    fun callPhone(phoneNum: String) {
        val intent = Intent(Intent.ACTION_CALL)
        val data = Uri.parse("tel:$phoneNum")
        intent.data = data
        showActivity(intent)
    }

}

